/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package th.stripes.util;

import java.util.List;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import th.config.ableContextListener;
import th.manager.UserManager;
import th.model.Akaun;
import th.model.AkaunFav;
import th.model.Deposit;
import th.model.Haji;
import th.model.Pengguna;
import th.service.session.LoginSession;
import th.service.session.SessionManager;

/**
 *
 * @author fikri
 */
public class AccCheckUtil {   
   
    
    private Pengguna pengguna;
    
    public boolean checkAcc(Deposit account, Pengguna pengguna) {
        
        if (account == null || account.getNoKp() == null || account.getNoAkaun() == null) return false;
        
        List<Akaun> akauns = pengguna.getSenaraiAkaun();
        
        boolean isValidAcc = false;
        boolean isValidKp = false;
        
        String[] noAcc = new String[akauns.size()];
        
        String[] noKp = new String[akauns.size()];        
        
        String noAcc1 = account.getNoAkaun();
        
        String noAcc2 = account.getNoAkaunDari();
        
        String noKp1 = account.getNoKp();
        
        String noKp2 = account.getNoKpDari();
        
        int i=0;
        
        for (Akaun akaun : akauns) {
            noAcc[i] = akaun.getAkaunTmbhn();          
            noKp[i] = akaun.getNoPengenalanTmbhn();
            i++;
        }
        
        if (ArrayUtils.contains(noAcc, noAcc1)){
            isValidAcc = ArrayUtils.contains(noAcc, noAcc2);
        }
        
        if (ArrayUtils.contains(noKp, noKp1)){
            isValidKp = ArrayUtils.contains(noKp, noKp2);
        }
        
        return (isValidAcc && isValidKp);
    }
    
     public boolean checkAcc(Haji account, Pengguna pengguna) {
        
        if (account == null || account.getNoKp() == null || account.getNoAkaun() == null) return false;
        
        List<Akaun> akauns = pengguna.getSenaraiAkaun();
        
        boolean isValidAcc = false;
        boolean isValidKp = false;
        
        String[] noAcc = new String[akauns.size()];
        
        String[] noKp = new String[akauns.size()];        
        
        String noAcc1 = account.getNoAkaun();
        
        String noAcc2 = account.getNoAkaunDari();
        
        String noKp1 = account.getNoKp();
        
        String noKp2 = account.getNoKpDari();
        
        int i=0;
        
        for (Akaun akaun : akauns) {
            noAcc[i] = akaun.getAkaunTmbhn();          
            noKp[i] = akaun.getNoPengenalanTmbhn();
            i++;
        }
        
        if (ArrayUtils.contains(noAcc, noAcc1)){
            isValidAcc = ArrayUtils.contains(noAcc, noAcc2);
        }
        
        if (ArrayUtils.contains(noKp, noKp1)){
            isValidKp = ArrayUtils.contains(noKp, noKp2);
        }
        
        return (isValidAcc && isValidKp);
    }
    

    
    public boolean checkAcc(Deposit deposit, String idPengguna) {
        
        SessionManager sm = SessionManager.getInstance();
        LoginSession ls = sm.getActiveSessionByUserId(idPengguna);
        if (ls == null) return false; 
        
        UserManager userManager = ableContextListener.newInstance(UserManager.class);
        pengguna = userManager.findUserByIdUser(idPengguna,"Y");
        
        if (pengguna == null) return false;
        
        return checkAcc(deposit, pengguna);
        
    }
    
        public boolean checkAcc(Haji haji, String idPengguna) {
        
        SessionManager sm = SessionManager.getInstance();
        LoginSession ls = sm.getActiveSessionByUserId(idPengguna);
        if (ls == null) return false; 
        
        UserManager userManager = ableContextListener.newInstance(UserManager.class);
        pengguna = userManager.findUserByIdUser(idPengguna,"Y");
        
        if (pengguna == null) return false;
        
        return checkAcc(haji, pengguna);
        
    }
    
    public Pengguna getPengguna() {
        return this.pengguna;
    }
    

    public Deposit checkPenjaga (Deposit account, Pengguna pengguna) {
    
       // if (account == null || account.getNoKp() == null || account.getNoAkaun() == null) return false;
        Deposit deposit = new Deposit();
        boolean isValidAcc = false;
        boolean isValidKp = false;
        
        if (account == null || account.getNoKp() == null || account.getNoAkaun() == null) {
            List<Akaun> akauns = pengguna.getSenaraiAkaun();
        
            

            String[] noAcc = new String[akauns.size()];

            String[] noKp = new String[akauns.size()];        

            String noAcc1 = account.getNoAkaun();

            String noAcc2 = account.getNoAkaunDari();

            String noKp1 = account.getNoKp();

            String noKp2 = account.getNoKpDari();

            int i=0;

            for (Akaun akaun : akauns) {
                noAcc[i] = akaun.getAkaunTmbhn();          
                noKp[i] = akaun.getNoPengenalanTmbhn();
                i++;
            }

            if (ArrayUtils.contains(noAcc, noAcc1)){
                isValidAcc = ArrayUtils.contains(noAcc, noAcc2);
            }

            if (ArrayUtils.contains(noKp, noKp1)){
                isValidKp = ArrayUtils.contains(noKp, noKp2);
            }
        }
        
        if ((isValidAcc && isValidKp) == true) {
           // deposit.setNoAkaunPenjaga(n);
        }
        
        return deposit;
    }
    
    
    public boolean checkAccFav(Deposit account, Pengguna pengguna){
    
        boolean isValidAcc = false;
        boolean isValidKp = false;
        
        if (account == null) return false;
        
        List<AkaunFav> akaunsFav = pengguna.getSenaraiAkaunFav();

        String[] noAccFav = new String[akaunsFav.size()];
        
        String[] noKp = new String[akaunsFav.size()];   
 
       String noAcc1 = account.getNoAkaunPenerima();
 
       String noKp1 = account.getNoKpDari();
        
        int i=0;
        
        for (AkaunFav akaun : akaunsFav) {
            
            noAccFav[i] = akaun.getNoAkaunFav();         
            noKp[i] = akaun.getPenggunaFav().getNoKp();
            i++;
        }
        
        if (ArrayUtils.contains(noAccFav, noAcc1)){
            isValidAcc = true;
        }
        
        if (ArrayUtils.contains(noKp, noKp1)){
            isValidKp = true;
        }
        
        return (isValidAcc && isValidKp); 
        //return (isValidAcc); 
    }
    
     public boolean checkAddAccFav(Deposit account, Pengguna pengguna){
    
        boolean isValidAcc = false;
       // boolean isValidKp = false;
        
        if (account == null) return false;

        List<AkaunFav> akaunsFav = pengguna.getSenaraiAkaunFav();
        String[] noAccFav = new String[akaunsFav.size()];
        String noAcc1 = account.getNoAkaunFav();
        int i=0;
        
        for (AkaunFav akaun : akaunsFav) {
            noAccFav[i] = akaun.getNoAkaunFav();         
            i++;
        }
        
        if (ArrayUtils.contains(noAccFav, noAcc1)){
            isValidAcc = true;
        }
        return (isValidAcc); 
    }
    
}
